import PyPDF2
from io import BytesIO

ALLOWED_EXTENSIONS = {'txt', 'pdf'}


def parse_file(file):
    if '.' not in file.filename or file.filename.split('.')[-1].lower() not in ALLOWED_EXTENSIONS:
        raise ValueError('Unsupported file type')

    content = BytesIO(file.read())

    if file.filename.endswith('.pdf'):
        reader = PyPDF2.PdfReader(content)
        text = '\n'.join([page.extract_text() for page in reader.pages])
    else:
        text = content.getvalue().decode('utf-8')

    return text.strip() or 'Empty file content'
